/**
 *字典
 *@wxch
 */
var codes = new Map();

function initselectcode(e) {
    //数据字典
    if (isBlank(e)) {
        e = $(document);
    }
    e.find('select[class*="select2-code"]').each(function () {
    	if($(this).attr('isload')!='true'){
    		var val = $(this).attr('value');
    		var htm = codes.get($(this).attr('zdlb') + '_code_html', val, 'option');
    		$(this).html('<option value="">请选择</option>' + htm);
    		$(this).change();
    		$(this).attr('isload','true');
    		initSelect($(this));
    	}
    });

    //解析select值显示
    $('.select2-code-name').each(function () {
        var val = $(this).html().replace(' ', '');
        var objname = codes.get_name($(this).attr('zdlb') + '_code_html', val);
        $(this).html(objname).removeClass('select2-code-name');
    });
}




//根据上级查下级
function code_subtype(subtype, tt) {

    if ($(tt).val() == '') {
        $('#' + subtype).html('<option value=""></option>');
    } else {
        var htm = codes.get_auto($(tt).attr('zdlb') + '_code_html', $('#' + subtype).attr('value'), $(tt).val(), 'option');
        $('#' + subtype).html('<option value=""></option>' + htm);
        $('#' + subtype).change();
    }

}
//封装Map对象
function Map() {
    var struct = function (key, id, value) {
        this.key = key;
        this.id = id;
        this.value = value;
    }

    var put = function (key, id, value) {
        for (var i = 0; i < this.arr.length; i++) {
            if (this.arr[i].key === key) {
                this.arr[i].id = id;
                this.arr[i].value = value;
                return;
            }
        }
        this.arr[this.arr.length] = new struct(key, id, value);
    }

    //获取一个数据字典的所有数据  并放到标签【tag】中
    var get = function (key, val, tag) {
        var htm = '';
        for (var i = 0; i < this.arr.length; i++) {
            if (this.arr[i].key.indexOf(key) ==0) {
                if (val == this.arr[i].id) htm += '<' + tag + ' value="' + this.arr[i].id + '" selected="selected">' + this.arr[i].value + '</' + tag + '>';
                else  htm += '<' + tag + ' value="' + this.arr[i].id + '">' + this.arr[i].value + '</' + tag + '>';
            }
        }
        return htm;
    }
    /**
     * 根据字典类别获取字典列表
     * @param zdlb 字典类别
     * @returns {Array} 数组
     */
    var getSysCodeByZdlb = function (zdlb) {
        var sysCode = [];
        for (var i = 0; i < this.arr.length; i++) {
            if (this.arr[i].key.indexOf(zdlb) ==0) {
                sysCode.push(this.arr[i]);
            }
        }
        return sysCode;
    }

    //获取一个数据字典的值以【auto】开头的所有数据  并放到标签【tag】中
    var get_auto = function (key, val, auto, tag) {
        var htm = '';
        for (var i = 0; i < this.arr.length; i++) {
            if (this.arr[i].key.indexOf(key) ==0 && this.arr[i].id.indexOf(auto) == 0) {
                if (val == this.arr[i].id) htm += '<' + tag + ' value="' + this.arr[i].id + '" selected="selected">' + this.arr[i].value + '</' + tag + '>';
                else  htm += '<' + tag + ' value="' + this.arr[i].id + '">' + this.arr[i].value + '</' + tag + '>';
            }
        }
        return htm;
    }

    //将字典值【val】转换成name
    var get_names = function (key, val) {
        var a = '';
        for (var i = 0; i < this.arr.length; i++) {
            if (this.arr[i].key.indexOf(key) ==0 && val.indexOf(',' + this.arr[i].id + ',') != -1) {
                a += this.arr[i].value + ',';
            }
        }
        return a;
    }

    //将字典值【val】转换成name
    var get_name = function (key, val) {
        var a = '';
        for (var i = 0; i < this.arr.length; i++) {
            if (this.arr[i].key.indexOf(key) ==0 && val == this.arr[i].id) {
                a = this.arr[i].value;
                break;
            }
        }
        return a;
    }

    var get_name_key = function (key) {
        var a = '';
        for (var i = 0; i < this.arr.length; i++) {
            if (this.arr[i].key == key) {
                a = this.arr[i].value;
                break;
            }
        }
        return a;
    }
    var get_id_key = function (key) {
        var a = '';
        for (var i = 0; i < this.arr.length; i++) {
            if (this.arr[i].key == key) {
                a = this.arr[i].id;
                break;
            }
        }
        return a;
    }

    var remove = function (key) {
        var v;
        for (var i = 0; i < this.arr.length; i++) {
            v = this.arr.pop();
            if (v.key === key) {
                continue;
            }
            this.arr.unshift(v);
        }
    }

    var getsize = function (key) {
        var a = 0;
        for (var i = 0; i < this.arr.length; i++) {
            if (this.arr[i].key.indexOf(key) ==0) {
                a++;
            }
        }
        return a;
    }
    var getkey = function () {
        var a = '';
        for (var i = 0; i < this.arr.length; i++) {
            var key = this.arr[i].key;
            key = key.substring(0, key.indexOf('_code_html'));
            if (a.indexOf(key) == -1) {
                a += key + ',';
            }
        }
        return a;
    }
    var getallkey = function () {
        var a = new Array();
        for (var i = 0; i < this.arr.length; i++) {
            a[i] = this.arr[i].key;
        }
        return a;
    }

    var size = function () {
        return this.arr.length;
    }

    var isEmpty = function () {
        return this.arr.length <= 0;
    }

    this.arr = new Array();
    this.get = get;
    this.get_auto = get_auto;
    this.get_name = get_name;
    this.get_names = get_names;
    this.getkey = getkey;
    this.get_name_key = get_name_key;
    this.get_id_key = get_id_key;
    this.getallkey = getallkey;
    this.getsize = getsize;
    this.put = put;
    this.remove = remove;
    this.size = size;
    this.isEmpty = isEmpty;
    this.getSysCodeByZdlb = getSysCodeByZdlb;
}
